package com.example.hackover_client;

import android.location.Criteria;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.view.View.OnClickListener;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.transport.HttpsTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class GoListener implements OnClickListener{

	private Main mainApp;
	private EditText et_un;
	private EditText et_pw;
	private RadioButton opt_M;
	private RadioButton opt_F;
	
	public GoListener(Main mainapp)
	{
		mainApp=mainapp;
		
		et_un = (EditText) mainApp.findViewById(R.id.et_un);
		et_pw = (EditText) mainApp.findViewById(R.id.et_pw);
		opt_M = (RadioButton) mainApp.findViewById(R.id.M);
		opt_F = (RadioButton) mainApp.findViewById(R.id.F);
	}
	
	public void AttachService()
	{
		LocationManager mngr = (LocationManager)mainApp.getSystemService(Context.LOCATION_SERVICE);
		Criteria crt= new Criteria();
		crt.setAccuracy(2);//medium
		String bestProvider = mngr.getBestProvider(crt, true);
		mngr.requestLocationUpdates(bestProvider,10000,10,new Runner(mainApp));
	}
		 
        public void onClick(View v) {
        	if(mainApp.IntroState == "login")
            {
        		Thread networkThread = new Thread() {
                    @Override
                    public void run() {
                      try {
                         SoapObject request = new SoapObject(mainApp.NAMESPACE, "AuthUser");    
                         request.addProperty("email", et_un.getText().toString());
                         request.addProperty("password", et_pw.getText().toString());
                         
                         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                         envelope.dotNet=true;
                         envelope.setOutputSoapObject(request);
                          
                         HttpTransportSE ht = new HttpTransportSE(mainApp.URL);
                         ht.call("http://tempuri.org/AuthUser", envelope);
                         final  SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
                         final String strResp = response.toString();
                 
                         mainApp.runOnUiThread (new Runnable(){ 
                     public void run() {
                          String [] splResp = strResp.split(":");
                          if(splResp[0].equals("OK")) 
                          	{
                          		mainApp.usr_GUID=splResp[1];
                          		mainApp.setContentView(R.layout.activity_main);
                          		AttachService();
                          	}
                          else{
                        	  et_un.setText("");
                        	  et_pw.setText("");
                          }
                           }
                       });
                      } 
                     catch (Exception e) {
                         e.printStackTrace();
                     }
                    }
                  };
                  networkThread.start();
                  
                  
                //broadcast status
            		Thread updateThread = new Thread() {
                        @Override
                        public void run() {
                          try {
                             SoapObject request = new SoapObject(mainApp.NAMESPACE, "GetList");    
                             request.addProperty("uGUID", mainApp.usr_GUID);
                             request.addProperty("lng","26.08165");
                             request.addProperty("lat","44.41962");
                             
                             SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                             envelope.dotNet=true;
                             envelope.setOutputSoapObject(request);
                              
                             HttpTransportSE ht = new HttpTransportSE(mainApp.URL);
                             ht.call("http://tempuri.org/GetList", envelope);
                             final  SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
                             final String strResp = response.toString();
                     
                             mainApp.runOnUiThread (new Runnable(){ 
                         public void run() {
                        	 TextView txtRez = (TextView) mainApp.findViewById(R.id.et_pw);
                      	   txtRez.setText(strResp.replace("|", "\n"));
                               }
                           });
                          } 
                         catch (Exception e) {
                             e.printStackTrace();
                         }
                        }
                      };
                      updateThread.start();
            }
            else if(mainApp.IntroState == "register")
            {
              Thread networkThread = new Thread() {
              @Override
              public void run() {
                try {
                   SoapObject request = new SoapObject(mainApp.NAMESPACE, "CreateUser");    
                   request.addProperty("email", et_un.getText().toString());
                   request.addProperty("password", et_pw.getText().toString());
                   
                   String sSex="";
                   if (opt_M.isChecked()==true) sSex="M";
                   else if (opt_F.isChecked()==true) sSex="F";
                   else return;
                   request.addProperty("sex", sSex);
                   
                   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                   envelope.dotNet=true;
                   envelope.setOutputSoapObject(request);
                    
                   HttpTransportSE ht = new HttpTransportSE(mainApp.URL);
                   ht.call("http://tempuri.org/CreateUser", envelope);
                   final  SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
                   final String strResp = response.toString();
           
                   mainApp.runOnUiThread (new Runnable(){ 
               public void run() {
                    String [] splResp = strResp.split(":");
                    if(splResp[0].equals("OK")) 
                    	{
                    		mainApp.usr_GUID=splResp[1];
                    		mainApp.setContentView(R.layout.activity_main);
                    		AttachService();
                    	}
                     }
                 });
                } 
               catch (Exception e) {
                   e.printStackTrace();
               }
              }
            };
            networkThread.start();
            }
        }
}
